package sedev.struts.portlet;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import sedev.shopping.business.PhamViService;
import sedev.shopping.business.ThongTinShopService;
import sedev.shopping.entities.ListPhamVi;
import sedev.shopping.entities.ThongTinShop;
import sedev.shopping.util.SedevUtil;

import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.Group;
import com.liferay.portal.model.Layout;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.opensymphony.xwork2.ActionSupport;

public class InfoShopAction extends ActionSupport implements
		ServletRequestAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;

	private void preExecute(){
		ListPhamVi listphamvi = new ListPhamVi();		
		listphamvi = PhamViService.getListDataByAll();
		this.request.setAttribute("listphamvi", listphamvi);
	}
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public String execute() throws Exception {
		this.preExecute();
		String actionResult = "";
		long userid = PortalUtil.getUserId(this.request);
		ThongTinShop thongtinshop = null;
		thongtinshop = ThongTinShopService.getDataByUser(userid);
		if (thongtinshop == null) {
			User user = UserLocalServiceUtil.getUserById(userid);
			this.request.setAttribute("owner", user);
			actionResult = ActionSupport.ERROR;
		} else {
			this.request.setAttribute("thongtinshop", thongtinshop);
			actionResult = ActionSupport.SUCCESS;
		}
		return actionResult;
	}

	public String add() throws Exception {
		this.preExecute();
		String actionResult = "";
		ThongTinShop thongtinshop = new ThongTinShop();
		try {
			if (this.initData(thongtinshop)) {
				if (ThongTinShopService.addData(thongtinshop)) {
					this.request.setAttribute("thongtinshop", thongtinshop);
					this.request.setAttribute("errorCode", 1);
					actionResult = ActionSupport.SUCCESS;
				} else {
					this.request.setAttribute("errorCode", -1);
					long userid = PortalUtil.getUserId(this.request);
					User user = UserLocalServiceUtil.getUserById(userid);
					this.request.setAttribute("owner", user);
					actionResult = ActionSupport.ERROR;
				}
			} else {
				this.request.setAttribute("errorCode", -1);
				long userid = PortalUtil.getUserId(this.request);
				User user = UserLocalServiceUtil.getUserById(userid);
				this.request.setAttribute("owner", user);
				actionResult = ActionSupport.ERROR;
			}
		} catch (Exception ex) {
			this.request.setAttribute("errorCode", -1);
			long userid = PortalUtil.getUserId(this.request);
			User user = UserLocalServiceUtil.getUserById(userid);
			this.request.setAttribute("owner", user);
			actionResult = ActionSupport.ERROR;
		}
		return actionResult;
	}

	public String update() throws Exception {
		this.preExecute();
		ThongTinShop thongtinshop = new ThongTinShop();
		try {
			thongtinshop.setId(Long.valueOf(this.request.getParameter("id")));
			if (thongtinshop.getId() != null) {
				if (this.initData(thongtinshop)) {
					if (ThongTinShopService.updateData(thongtinshop)) {
						this.request.setAttribute("errorCode", 2);
					} else {
						this.request.setAttribute("errorCode", -1);
					}
				} else {
					this.request.setAttribute("errorCode", -1);
				}
			} else {
				this.request.setAttribute("errorCode", -1);
			}
		} catch (Exception ex) {
			this.request.setAttribute("errorCode", -1);
		}
		this.request.setAttribute("thongtinshop", thongtinshop);
		return ActionSupport.SUCCESS;
	}

	private boolean initData(ThongTinShop data) {
		try {
			data.setTenShop(this.request.getParameter("title"));
		} catch (Exception ex) {
			data.setTenShop("");
		}
		try {
			data.setSlogan(this.request.getParameter("slogan"));
		} catch (Exception ex) {
			data.setSlogan("");
		}
		try {
			data.setDiaChi(this.request.getParameter("address"));
		} catch (Exception ex) {
			data.setDiaChi("");
		}
		try {
			data.setChuShop(this.request.getParameter("ownername"));
		} catch (Exception ex) {
			data.setChuShop("");
		}
		try {
			data.setEmail(this.request.getParameter("email"));
		} catch (Exception ex) {
			data.setEmail("");
		}
		try {
			data.setPhone(this.request.getParameter("phone"));
		} catch (Exception ex) {
			data.setPhone("");
		}
		try {
			data.setNgayDang(SedevUtil.getTime());
		} catch (Exception ex) {
			data.setNgayDang(Long.valueOf(0));
		}
		try {
			data.setPhamViId(Integer.valueOf(this.request
					.getParameter("location")));
		} catch (Exception ex) {
			data.setPhamViId(0);
		}

		data.setSoSanPhamTrongNgay(5);
		data.setSoSanPhamVipTrongNgay(1);

		data.setHienThi(true);
		data.setUserId(PortalUtil.getUserId(this.request));
		ThemeDisplay themeDisplay = (ThemeDisplay) request
				.getAttribute(WebKeys.THEME_DISPLAY);
		try {
			Layout layout = themeDisplay.getLayout();
			if (!layout.getPrivateLayout()) {
				Group group = layout.getGroup();
				data.setGroupId(group.getGroupId());
			} else {
				return false;
			}
		} catch (Exception ex) {
			data.setGroupId(Long.valueOf(0));
			return false;
		}
		return true;
	}
}
